<?php
/**
 * Describe:
 * Source:
 * User: blue
 * Time: 2018/1/28 16:24
 */

namespace app\auth\controller;

use app\common\controller\Base;
use think\Db;
use think\Validate;
use think\View;

class Member extends Base
{
    protected $modelName='member';
    // protected $pk='mid';

    public function listC(){
        $res=$this->model->show();
        isset($res['data'])&&View::share('data',$res['data']);
        return view();
    }
    //添加方法
    public function addC(){
        if ($this->request->isPost()){
            //数据入库前的几项操作,1.获取+过滤2.字段转换3.验证器验证
            $data=$this->request->param('','','strip_tags,trim');
            $check=$this->validation('\aop\validate\Member',$data,'add');
            if($check['status']==1) {
                //add 方法可以进行扩充,添加必要的参数
                $res=$this->model->add($data);
                ($res['status']=='success')?$this->success($res['msg'],'listC'):$this->error($res['msg'],'listC');
            }
        }
        return view();
    }
    //删除方法
    public function delC(){
        if($this->request->isAjax()){
            $data=$this->request->post();
            $check=$this->validation('\aop\validate\Member',$data,'del');
            //验证失败,直接返回
            if($check['status']==0){
                return json(['msg'=>$check['msg']]);
            }
            $res=$this->model->del($data['id']);
            return json($res);
        }
    }

    public function editC(){
        //旧数据分配
        $where['id']=intval($this->request->param('id'));
        $oldData=$this->model->show($where);
        isset($oldData['data'])&& View::share('oldData',$oldData['data'][0]);
        if($this->request->isPost()){
            $data=$this->request->post('','','strip_tags');
            $data['id']=$where['id'];
            $check=$this->validation('\aop\validate\Member',$data,'edit');
            //验证后的数据提交方法
            if($check['status']==1){
                $res=$this->model->edit($data);
                ($res['status']=='success')?$this->success($res['msg'],'listC'):$this->error($res['msg'],'listC');
            }
        }
        return view();
    }
    /**
     * 修改密码
     */
    public function changePasswrod()
    {
        //获取旧数据
        $where['id'] = intval($this->request->param('id'));
        $res = $this->model->show($where);
        isset($res['data']) && View::share('oldData', $res['data'][0]);
        /*先判断旧密码是否正确*/
        if ($this->request->isPost()) {
            $data = $this->request->param('', '', 'strip_tags,trim');
            $data['id'] = $where['id'];
            //验证器验证数字,可以做成一个函数
            $this->validation('\aop\validate\Member', $data, 'repwd');
            /*验证旧密码是否正确*/
            $resData = \app\auth\model\Member::get($data['id'])->password;
            $bool = password_verify($data['oldPassword'], $resData);
            if (!$bool) {
                $this->error('旧密码不正确');
            }
            /*更新数据库*/
            $password = password_hash($data['password'], PASSWORD_DEFAULT);
            $upResult = Db::table('addons_member')->where('id', $data['id'])->update(['password' => $password]);
            if ($upResult) {
                $this->success('密码更新成功');
            } else {
                $this->error('密码更新失败');
            }
        }
        return view();
    }
    /**
     *
     */
    public function setGroup(){
        //查找所有的会员组
        $id=$this->request->param('id');
        $auth=$this->tableSelect((new \app\auth\model\Group()));
        $myAuth=Db::table('addons_auth_group_access')->field('group_id')->where('uid',$id)->select();
        $myAuth=array_column($myAuth,'group_id');
        View::share(['auth'=>$auth,'myAuth'=>$myAuth]);
        if($this->request->isPost()){
            //接收过滤参数->转换字段->验证
            $data=$this->request->post();
            if(!empty($data)){
                $data=$data['group_id'];
                array_walk($data,function(&$value,$key) use($id){
                    $value=['uid'=>$id,'group_id'=>$value];
                });
            }
            //中间表进行事务操作
            Db::startTrans();
            try{
                Db::table('addons_auth_group_access')->where('uid',$id)->delete();
                Db::table('addons_auth_group_access')->insertAll($data);
            }catch(\Exception $e){
                Db::rollback();
                $this->error("执行失败:".$e->getMessage());
            }
            $this->success('操作成功','listC');
        }
        return view();
    }
}